#ifndef _DANIEL_KERR_GLOPTIONS_H
#define _DANIEL_KERR_GLOPTIONS_H

#include "driver.h"
#include "sliderbar.h"
#include "checkbox.h"
#include "textbox.h"
#include "glstar.h"
#include "gltext.h"

struct graphOptions
{
	graphOptions()
	{
		aliasing = true;
		fps = 40;
		gldetail = 0.5;
		starcount = 50;
	}

	bool aliasing;
	int fps;
	GLfloat gldetail;
	int starcount;
};

GAMESTATE displayGraphOptionPage(graphOptions &reply)
{
	glClearColor(0.0,0.0,0.0,0.0);
	
	glDisable(GL_DEPTH_TEST);	
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluOrtho2D(0,X_DIM,Y_DIM,0);

	textBox* t = new textBox(125,510,120," OK");
	textBox* q = new textBox(300,510,120," Cancel");

	float r = 0.2,g = 0.2,b = 0.2;
	int mult = 5;

	Star_C belt;
	belt.set2Dstars(2000);

	checkBox alias(125,220,30,CHECKBOX_MINIBOX);
	sliderBar frames(125,295,0,255,1,550,30,2);
	sliderBar detail(125,370,0,255,1,550,30,2);
	sliderBar stars(125,445,0,255,1,550,30,2);

	alias.setData(reply.aliasing);
	frames.setData((((float)reply.fps-30.0)/20.0)*255.0);
	detail.setData((float)reply.gldetail*255.0);
	stars.setData((((float)reply.starcount-50.0)/100.0)*255.0);

	Timer fps;
	SDL_Event event;
	while(true)
	{
		fps.start();

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		for(int i = 0; i < belt.NOS(); ++i)
		{
			if(i % 2 == 0)		{glColor4f(r, g, b, 0.9);}
			else				{glColor4f(1-r, 1-g, 1-b, 0.9);}

			glTranslatef(belt.sx(i),belt.sy(i),belt.sz(i));
			glutSolidSphere(belt.ss(i),10,10);
			glTranslatef(-belt.sx(i),-belt.sy(i),-belt.sz(i));
		}

		glLineWidth(8);
		glColor4f(0.5-r,0.5-g,0.5-b,1.0);
		writeText(0,200,2.0,2.0,"Options");
		glLineWidth(3.2);
		glColor4f(1-r,1-g,1-b,1.0);
		writeText(102,147,0.5,0.5,"Options");
		glColor4f(r,g,b,1.0);
		writeText(100,150,0.5,0.5,"Options");
		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		writeText(125,170,0.1,0.1,"by the Mogs of War");
		writeText(0,600,0.1,0.1,"2007 Texas Tech University Computer Science");
		
		r += (0.001 * mult);
		g += (0.001 * mult);
		b += (0.001 * mult);

		if((r > 1)||(g > 1)||(b > 1)) mult = mult * -1;
		if((r < 0.2)||(g < 0.2)||(b < 0.2)) mult = mult * -1;

		t->draw();
		q->draw();

		char buf[20];
		string temp;

		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		writeText(125,215,0.1,0.1,"Anti Aliasing ("+(string)((alias.getData()==1.0)?"on":"off")+")");\
		_itoa_s((int)(((frames.getData()/255.0)*20.0)+30.0),buf,20,10);
		temp = "Frames Per Second (" + string(buf) + ")";
		writeText(125,290,0.1,0.1,temp);
			writeText(100,315,0.1,0.1,"30");
			writeText(685,315,0.1,0.1,"50");
		_itoa_s((int)(detail.getData()/2.55),buf,20,10);
		temp = "Detail Level (" + string(buf) + ")";
		writeText(125,365,0.1,0.1,temp);
			writeText(100,390,0.1,0.1,"0%");
			writeText(685,390,0.1,0.1,"100%");
		_itoa_s((int)(((stars.getData()/255.0)*100.0)+50.0),buf,20,10);
		temp = "Background Stars (" + string(buf) + ")";
		writeText(125,440,0.1,0.1,temp);
			writeText(100,465,0.1,0.1,"50");
			writeText(685,465,0.1,0.1,"150");

		alias.draw();
		frames.draw();
		detail.draw();
		stars.draw();

		SDL_GL_SwapBuffers();

		if(t->getData() == 1)
		{
			reply.aliasing = (alias.getData()==1.0);
			reply.fps = ((frames.getData()/255.0)*20.0)+30.0;
			reply.gldetail = detail.getData()/255.0;
			reply.starcount = ((stars.getData()/255.0)*100.0)+50.0;
			return TITLE;
		}
		else if(q->getData() == 1)
		{return TITLE;}

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_o:
					case SDLK_RETURN:
						reply.aliasing = (alias.getData()==1.0);
						reply.fps = ((frames.getData()/255.0)*20.0)+30.0;
						reply.gldetail = detail.getData()/255.0;
						reply.starcount = ((stars.getData()/255.0)*100.0)+50.0;
						return TITLE; break;
					case SDLK_c:
					case SDLK_q:
					case SDLK_ESCAPE:
						return TITLE; break;
				}
			}
			if(event.type == SDL_MOUSEBUTTONDOWN)
			{
				if(alias.clicked(event.button.x,event.button.y))
				{alias.onClick();}
				if(frames.clicked(event.button.x,event.button.y))
				{frames.onClick();}
				if(detail.clicked(event.button.x,event.button.y))
				{detail.onClick();}
				if(stars.clicked(event.button.x,event.button.y))
				{stars.onClick();}
				if(t->clicked(event.button.x, event.button.y))
				{t->onClick();}
				if(q->clicked(event.button.x, event.button.y))
				{q->onClick();}
			}
			if(event.type == SDL_MOUSEMOTION)
			{
				t->setHover(t->hover(event.motion.x,event.motion.y));
				q->setHover(q->hover(event.motion.x,event.motion.y));
			}
		}

		// Cap Frame Rate
		while(fps.get_ticks()<1000/35){}
	}
}

#endif